/*
 * tcp_send.c
 *
 *  Created on: May 7, 2016
 *      Author: root
 */
#include "tcp_send_cmd.h"

#include <pthread.h>
#include "../../util/log.h"
#include "tcp_helper.h"
#include "../../util/str_util.h"
#include "json_helper.h"

#define TCP_SEND_SOCKET_PORT  13427
#define CMD_ID_NOTIFYDEVICESTATE 	1
#define CMD_ID_NOTIFYNETSTATUS 		2

int is_tcp_send_run;
int serv_send_socket_fd;
int client_send_socket_fd;

void tcp_send_accept_client_connect();
void tcp_send_handle_response(char* json_str);
void handle_rep_of_notifyDeviceState(char* json_str);
void send_notify_data_to_client(char* json_send_str);

/**
 * start send cmd tcp server
 */
int start_tcp_send_cmd_server() {
	LogStr("tcp_send_start_cmd_server()!--->>> \n ");
	is_tcp_send_run = 1;
	serv_send_socket_fd = init_tcp_socket(TCP_SEND_SOCKET_PORT);
	if(serv_send_socket_fd <= 0){
		Log("init send server socket failed!--->>socket_fd = d%\n", serv_send_socket_fd);
	}
	Log("init send server socket success!--->>socket_fd = d%\n", serv_send_socket_fd);
	pthread_t sub_thread;
	int ret = pthread_create(&sub_thread, NULL, (void *)tcp_send_accept_client_connect, NULL);
	if(ret == -1){
		LogStr("creat send server sub thread failed! \n ");
	}
	return serv_send_socket_fd;
}


void tcp_send_accept_client_connect(){
	LogStr("tcp_send_accept_client_connect()--->>>>\n");
	while(is_tcp_send_run){
		LogStr("tcp send server socket accept before--->>>\n");
		client_send_socket_fd = accept(serv_send_socket_fd, NULL, NULL);
		Log("tcp send server socket accept after--->>>client_socket_fd = %d\n", client_send_socket_fd);
		if(client_send_socket_fd == 0) {
			LogStr("tcp send server socket accept client failed !");
			continue;
		}else if(client_send_socket_fd < 0){
			LogStr("tcp send server socket accept client failed !");
			return;
		}

	}
	close(serv_send_socket_fd);
	close(serv_send_socket_fd);
}



void send_notify_data_to_client(char* json_send_str){
	int send_len = send_data_to_client(client_send_socket_fd, json_send_str);
	if(send_len > 0){
		char* json_rep_str;
		int content_len = recv_data_from_client(&json_rep_str, client_send_socket_fd);
		if(content_len > 0){
			tcp_send_handle_response(json_rep_str);
			free(json_rep_str);
		}else{
			client_send_socket_fd = -1;
		}
	}else{
		client_send_socket_fd = -1;
	}
}



void notify_device_state(DeviceState device_state){
	if(client_send_socket_fd > 0){
		char* json_send_str = get_json_str_of_notify_device_state(device_state);
		send_notify_data_to_client(json_send_str);
		free(json_send_str);
	}
}

void notify_net_status(NetStatus net_status){
	if(client_send_socket_fd > 0){
		char* json_send_str = get_json_str_of_notify_net_status(net_status);
		send_notify_data_to_client(json_send_str);
		free(json_send_str);
	}
}



void tcp_send_handle_response(char* json_str){
	if(json_str == NULL){
		return ;
	}
	Log("json_str_len =  %d\n", strlen(json_str));
	Log("json_str = %s\n", json_str);
	int cmd_id = parse_req_to_get_cmd_id(json_str);
	Log("cmd_id = %d\n", cmd_id);

	switch(cmd_id){
		case CMD_ID_NOTIFYDEVICESTATE:
			handle_rep_of_notifyDeviceState(json_str);
			break;
		case CMD_ID_NOTIFYNETSTATUS:
			handle_rep_of_notifyNetStatus(json_str);
			break;
	}
}


void handle_rep_of_notifyDeviceState(char* json_str){
	int rep_status = get_app_res_status_by_json(json_str);

	Log("rep_status = %d\n", rep_status);
}


void handle_rep_of_notifyNetStatus(char* json_str){
	int rep_status = get_app_res_status_by_json(json_str);

	Log("rep_status = %d\n", rep_status);
}






